package com.youtui.manager.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.youtui.common.constant.ResultMessage;
import com.youtui.common.constant.SystemConstant;
import com.youtui.common.util.DxSmsUtil;
import com.youtui.common.util.ResultUtil;
import com.youtui.manager.service.ISmsCodeService;

@Controller
@RequestMapping("/smscode")
public class SmsCodeController extends BaseController {

	@Autowired
	DxSmsUtil dxSmsUtil;
	@Autowired
	ISmsCodeService smsCodeService;

	@RequestMapping("/verify.do")
	@ResponseBody
	public ResultMessage verify(String mobile) throws Exception {

		// 验证码
		int count = smsCodeService.findCodeSendByMobile(mobile, SystemConstant.SmsCode_Type_login);
		if (count > 0) {
			return ResultUtil.result(ResultUtil.code_fail, "验证码有效期10分钟，请勿重复请求");
		}

		String code = smsCodeService.insertSmsCode(mobile, SystemConstant.SmsCode_Type_login); // 登录短信验证码

		String message = "您的登录验证码为：" + code + ",有效期10分钟， 欢迎使用优推互动广告平台！";
		dxSmsUtil.sendSmsMessage_Hex(mobile, message);

		return ResultUtil.result(ResultUtil.code_success, "验证码发送成功");
	}

	@RequestMapping("/verify_update.do")
	@ResponseBody
	public ResultMessage verifyUpdate(String mobile) throws Exception {

		// 验证码
		int count = smsCodeService.findCodeByMobile(mobile, SystemConstant.SmsCode_Type_update);
		if (count > 0) {
			return ResultUtil.result(ResultUtil.code_fail, "验证码有效期10分钟，请勿重复请求");
		}

		String code = smsCodeService.insertSmsCode(mobile, SystemConstant.SmsCode_Type_update); // 登录短信验证码

		String message = "您的验证码为：" + code + ",有效期10分钟， 欢迎使用优推互动广告平台！";
		dxSmsUtil.sendSmsMessage_Hex(mobile, message);

		return ResultUtil.result(ResultUtil.code_success, "验证码发送成功");
	}

	@RequestMapping("/confirm.do")
	@ResponseBody
	public ResultMessage confirm(String mobile, String smsCode) throws Exception {

		Boolean flag = smsCodeService.checkSmsCode(mobile, smsCode, SystemConstant.SmsCode_Type_login);

		if (flag) {
			return ResultUtil.result(ResultUtil.code_success, "验证成功");
		}
		return ResultUtil.result(ResultUtil.code_fail, "验证失败");
	}

}
